//
//  RadioViewController.m
//  Leisure
//
//  Created by xalo on 16/6/30.
//  Copyright © 2016年 蓝鸥科技有限公司西安分公司. All rights reserved.
//

#import "RadioViewController.h"

@interface RadioViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong,nonatomic)NSMutableArray *scrollArray;
@property (strong,nonatomic)NSMutableArray *dataSource;
@property (nonatomic,assign)NSInteger newestStart;


@end

@implementation RadioViewController
-(NSMutableArray *)scrollArray{
    if (!_scrollArray) {
        _scrollArray = [NSMutableArray array];
    }
    return _scrollArray;
}
-(NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerNib:[UINib nibWithNibName:@"RadioCell" bundle:nil] forCellReuseIdentifier:@"radio_cell"];
    [RequestManager fetchDataWithUrl:kRadioUrl parameter:@{} method:@"POST" completed:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
//        NSLog(@"%@",dic);
        for (NSDictionary *url in dic[@"data"][@"carousel"]) {
            [self.scrollArray addObject:url[@"img"]];
        }
        //        NSLog(@"%@",self.scrollArray);
        for (NSDictionary *diction in dic[@"data"][@"alllist"]) {
            RadioModel *model = [[RadioModel alloc]init];
            [model setValuesForKeysWithDictionary:diction];
            [self.dataSource addObject:model];
        }
    } updateUI:^{
        //创建轮播图
        ScrollView *headView = [[ScrollView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight/3) imageArray:self.scrollArray];
//        headView.backgroundColor = [UIColor redColor];
        self.tableView.tableHeaderView = headView;
        [self.tableView reloadData];
    }];
    self.tableView.mj_header = [MJRefreshStateHeader headerWithRefreshingBlock:^{
        [self loadViewWithStart:0 limit:10];
    }];
    self.tableView.mj_footer = [MJRefreshBackGifFooter footerWithRefreshingBlock:^{
        self.newestStart += 10;
        [self loadViewWithStart:self.newestStart limit:10];
    }];
    


}



//向下刷新数据
-(void)loadViewWithStart:(NSInteger)start limit:(NSInteger)limit{
    
        [RequestManager fetchDataWithUrl:kRadioURL parameter:@{} method:@"POST" completed:^(NSData *data) {
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
//            NSLog(@"%@",dic);
            for (NSDictionary *url in dic[@"data"][@"carousel"]) {
                [self.scrollArray addObject:url[@"img"]];
            }
    //        NSLog(@"%@",self.scrollArray);
    
            for (NSDictionary *diction in dic[@"data"][@"alllist"]) {
                RadioModel *model = [[RadioModel alloc]init];
                [model setValuesForKeysWithDictionary:diction];
                [self.dataSource addObject:model];
            }
    
        } updateUI:^{
            //创建轮播图
//            ScrollView *headView = [[ScrollView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight/3) imageArray:self.scrollArray];
//            headView.backgroundColor = [UIColor redColor];
//            self.tableView.tableHeaderView = headView;
            [self.tableView.mj_footer endRefreshing];
            
             [ self.tableView reloadData];
        }];
    
    
}


//上拉加载数据






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


#pragma mark ---UITableViewDelegate---
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    RadioCell *cell = [tableView dequeueReusableCellWithIdentifier:@"radio_cell" forIndexPath:indexPath];
    RadioModel *model = self.dataSource[indexPath.row];
    [cell setCellWithModel:model];
    
    
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 130;
}
#pragma mark tabelView点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    RadioListViewController *radioVC = [[RadioListViewController alloc]initWithNibName:@"RadioListViewController" bundle:nil];
    radioVC.radioModel = self.dataSource[indexPath.row];
    [self.navigationController pushViewController:radioVC animated:YES];
    
    
}
@end
